<template>
	<view style="padding: 100px 20px;">
		<button @tap="startRecord">开始录音</button>
		<button @tap="endRecord">停止录音</button>
		<div>
			<div>一句话识别结果：</div>
			<div>{{ result }}</div>
		</div>
	</view>
</template>
<script>
	// @ts-ignore
	const recorderManager = uni.getRecorderManager();
	import sendVoice from './sendVoice.js';
	const {
		authForApp
	} = require('../component/demo/appPermission');

	// @ts-ignore
	export default {
		data() {
			return {
				// 填入secretKey和secretId就可以直接使用了
				secretKey: 'V2X3d8ulhirvtkVFtPygTJa0Nl6bCHVe',
				secretId: 'AKIDDYp3Nf0FR4vpeNzW9gK1oYSMCHONMKNB',
				appId: '1323065199',

				// secretKey: '',
				// secretId: '',
				// appId: '',
				voicePath: '',
				// 一句话录音结果
				result: '',
				// 更多参数参考腾讯文档,全部兼容
				defaultData: {
					EngSerViceType: '16k_en',
					SourceType: 1,
					VoiceFormat: 'mp3',
					Data: '',
					DataLen: null
				}
			};
		},
		onLoad() {
			this.dealStop();
		},
		mounted() {
			authForApp('麦克风权限')
		},
		methods: {
			startRecord() {
				console.log('开始录音');

				recorderManager.start();
			},
			endRecord() {
				console.log('录音结束');
				recorderManager.stop();
			},
			async dealStop() {
				recorderManager.onStop(async path => {
					console.log('%c监听录音结束 Line:47 🍖 res', 'color:#465975', path);
					// 获取临时路径
					this.voicePath = path.tempFilePath;
					console.log('%c Line:59 🍰 this.voicePath', 'color:#4fff4B', this.voicePath);
					// 临时路径转base64
					const base64 = await this.pathToBase64(this.voicePath);
					console.log('%c Line:55 🌮 base64', 'color:#f5ce50', base64);
					// base64录音内容
					this.defaultData.Data = base64;
					// 录音长度
					this.defaultData.DataLen = uni.base64ToArrayBuffer(base64).byteLength;
					console.log('%c Line:60 🍺 this.defaultData.DataLen', 'color:#2eafb0', this.defaultData
						.DataLen);
					const {
						secretId,
						secretKey,
						defaultData
					} = this;
					// 一句话识别返回结果
					const res = await sendVoice({
						secretId,
						secretKey,
						payload: defaultData
					});
					this.result = res.Response.Result;
					console.log('%c Line:58 🍕 res', 'color:#3f7cff', res);
				});
			},
			async pathToBase64(path) {
				console.log('读取文件：', path);
				const base64 = await new Promise((resolve, reject) => {
					plus.io.resolveLocalFileSystemURL(
						path,
						function(entry) {
							entry.file(
								function(file) {
									// @ts-ignore
									var fileReader = new plus.io.FileReader();
									fileReader.onload = function(evt) {
										console.log('%c Line:80 🌭 evt', 'color:#3f7cff', evt);
										let result = evt.target.result;
										result = result.replace('data:audio/mpeg;base64,', '');
										resolve(result);
									};
									fileReader.onerror = function(error) {
										console.log('failed: ', error);
										reject(error);
									};
									fileReader.readAsDataURL(file);
								},
								function(error) {
									console.log('failed: ', error);
									reject(error);
								}
							);
						},
						function(error) {
							console.log('failed: ', error);
						}
					);
				});
				return base64;
			}
		}
	};
</script>